package com.copsoftware.utils.beans;

import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateList;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;

public class GeoUtils {

	public static Coordinate obtenerPunto(String latitud, String longitud) {
		Double x = Double.parseDouble(latitud);
		Double y = Double.parseDouble(longitud);
		Coordinate c = new Coordinate(x, y);
		return c;
	}

	public static Geometry obtenerPolygono(String cadenaPuntos) {

		if (!StringUtils.hasText(cadenaPuntos))
			return null;
		CoordinateList coordinateList = new CoordinateList();
		obtenerCoordinateList(cadenaPuntos, coordinateList);
		agregarFirstCoordinate(coordinateList);
		LinearRing lr = new GeometryFactory().createLinearRing(coordinateList
				.toCoordinateArray());
		return new GeometryFactory().createPolygon(lr, null);
	}

	public static Geometry[] obtenerPoint(String cadenaPuntos) {
		CoordinateList coordinateList = new CoordinateList();
		obtenerCoordinateList(cadenaPuntos, coordinateList);
		Geometry [] arrPoints= new Geometry[coordinateList.size()];
		for (int i = 0; i < coordinateList.toArray().length; i++) {
			Coordinate c = coordinateList.getCoordinate(i);	
//			Coordinate c = coordinateList.getCoordinate(0);
			arrPoints[i] = new GeometryFactory().createPoint(c);			
		}
//		return new GeometryFactory().createPoint(c);
		return arrPoints;
	}

	public static Geometry obtenerPunto(String cadenaPuntos) {
		return obtenerPuntos(cadenaPuntos)[0];
	}
	public static Geometry [] obtenerPuntos(String cadenaPuntos) {		
		if (!StringUtils.hasText(cadenaPuntos))
			return null;
		return obtenerPoint(cadenaPuntos);
	}

	private static void agregarFirstCoordinate(CoordinateList coordinateList) {
		if (coordinateList.size() > 0) {
			Coordinate firstCoordinate = coordinateList.getCoordinate(0);
			coordinateList.add(firstCoordinate, true);
		}
	}

	private static void obtenerCoordinateList(String cadenaPuntos,
			CoordinateList coordinateList) {
		StringTokenizer cadenaTokens = new StringTokenizer(cadenaPuntos, ",");
		while (cadenaTokens.hasMoreTokens()) {
			String stringTokenned = cadenaTokens.nextToken();
			String[] arrPunto = null;
			if (StringUtils.hasText(cadenaPuntos))
				arrPunto = stringTokenned.split(" ");
			Coordinate c = obtenerPunto(arrPunto[0], arrPunto[1]);
			coordinateList.add(c, true);
		}
	}

	// POLYGON ((-12.095115740552078 -77.0656156539917, -12.096857185723275
	// -77.06692457199097, -12.096731298500893 -77.06761121749878,
	// -12.098514695288896 -77.06825494766235, -12.099458841712556
	// -77.0687484741211, -12.099605708634263 -77.06986427307129,
	// -12.098053111379116 -77.07164525985718, -12.09746563979571
	// -77.07239627838135, -12.096668354867479 -77.07353353500366,
	// -12.096353636478105 -77.0751428604126, -12.095640273423898
	// -77.07692384719849, -12.094612188027018 -77.07741737365723,
	// -12.090457843514256 -77.075936794281, -12.093122506254717
	// -77.06838369369507, -12.09335330256553 -77.06776142120361,
	// -12.094863964408086 -77.06585168838501, -12.095115740552078
	// -77.0656156539917))
	public static String obtenerGPolygon(String wkt) {
		StringTokenizer cadenaTokens = new StringTokenizer(wkt, ",");
		Coordinate c = null;
		String sGPolygon = "new GPolygon([";
		while (cadenaTokens.hasMoreTokens()) {
			String stringTokenned = cadenaTokens.nextToken();
			sGPolygon += obtenerGLatLng(stringTokenned);
			if (cadenaTokens.hasMoreTokens())
				sGPolygon+=",";
		}
		return sGPolygon+"],";
	}

	public static String obtenerGLatLng(String wkt) {
		final Log logger = LogFactory.getLog(GeoUtils.class);
		
		StringTokenizer cadenaTokens = new StringTokenizer(wkt, "(),");
		Coordinate c = null;
		logger.info("Wkt "+wkt+"\n");
		while (cadenaTokens.hasMoreTokens()) {
			String stringTokenned = cadenaTokens.nextToken();
			logger.info("String Tokenned "+stringTokenned);
			String[] arrPunto = null;
			if (StringUtils.hasText(wkt)) {
				stringTokenned = StringUtils.trimLeadingWhitespace(stringTokenned);
				arrPunto = stringTokenned.split(" ");
			}

			if (stringTokenned.indexOf(".") < 0)
				continue;
			c = obtenerPunto(arrPunto[0], arrPunto[1]);
		}
		return "new GLatLng(" + c.x + "," + c.y + ")";
	}

	// POINT (123.123 312.123)

	public static void main(String[] args) throws Exception {
		// Coordinate c = new Coordinate(123.123,312.123);
		// Geometry g= new GeometryFactory().createPoint(c);
		// System.out.println("Point : "+g.toString()+"\n");
		// System.out.println(obtenerGLatLng(g.toString()));

	}

}
